<?php
/**
 * 获奖结果控制器
 */
namespace Beerus\Modules\DesignerCompetition2022\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\DesignerCompetition\DcHonor;
use Doraemon\model\ReDesignerCompetition\Result\DcUserHonorHelper;
use Doraemon\model\DesignerCompetition2021\audit\admin\DcAuditFirstWorkAdmin;
use Doraemon\model\ReDesignerCompetition\Sign\DcUserSignHelper;
use Doraemon\model\ReDesignerCompetition\Sign\Sign2022\DcUserInfo;
use Doraemon\pockets\constInterface\Designer\DesignerCompetition2022;
use Doraemon\service\DesignerCompetition2021\AwardService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;

class HonorResultController extends Controller implements DesignerCompetition2022 {
    const PAGE_SIZE = 10;

    public function index(Request $request, Content $content) {
        $page = $request->get('page', 1);
        $pageSize = $request->get('per_page', self::PAGE_SIZE);
        $request = $request->all();
        $condition = [];
        if (!empty($request['uid'])) {
            $condition['uid'] = $request['uid'];
        }
        if (!empty($request['honor_id'])) {
            $condition['honor_id'] = $request['honor_id'];
        }
        $total = (DcUserHonorHelper::getInstance(self::SIGN_YEAR))->getCount($condition);
        $condition['ORDER'] = ['id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $list = (DcUserHonorHelper::getInstance(self::SIGN_YEAR))->getListByCondition($condition);
        self::formatList($list);
        // 获取所有奖项
        $data = DcHonor::getHonorByYear(self::SIGN_YEAR);
        // 所有奖项的ID
        $honorList = array_column($data, 'honor_name', 'id');
        $gridModel = new pGridDataModel($list, $total, [
            'perPage' => self::PAGE_SIZE,
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel, $honorList) {
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->filter(function (Grid\Filter $filter) use ($honorList) {
                    $filter->disableIdFilter();
                    $filter->column(4, function ($filter) {
                        $filter->equal('uid', '设计师UID')->placeholder('请输入uid');
                    });
                    $filter->column(4, function ($filter) use ($honorList) {
                        $filter->equal('honor_id', '获得奖项')->select($honorList);
                    });
                    $filter->expand();
                });

                $grid->column('id', 'ID');
                $grid->column('uid', '设计师UID');
                $grid->column('nick', '设计师参赛昵称');
                $grid->column('honor_name', '获得奖项');
                $grid->column('honor_type', '奖项类型');
                $grid->column('work_name', '作品名称');
                $grid->column('poster', '奖项证书')->width(50)->display(function () {
                    if ($this->cert_pic) {
                        return '<a href="' . $this->cert_pic . '" target="_blank">查看</a>';
                    }
                });
                $grid->disableActions();
            }
        );
        return $content
            ->header('获奖结果')
            ->row($gridList);
    }

    public static function formatList(&$list) {
        if (empty($list)) return;
        // 获取所有奖项
        $data = DcHonor::getHonorByYear(self::SIGN_YEAR);
        // 所有奖项的ID
        $honorIds = array_column($data, 'id');
        // 奖项ID与奖名称
        $honorNameToId = array_column($data, 'honor_name', 'id');
        // 获取所有作品详情
        // 所有获奖的作品ID
        $signIds = array_unique(array_column($list, 'sign_id'));
        $signDatas = (DcUserSignHelper::getInstance(self::SIGN_YEAR))->getAllSignByIds($signIds);
        $signDatasMap = array_column($signDatas, null, 'id');
        // 获取用户昵称
        $uids = array_unique(array_column($list, 'uid'));
        $userInfoList = DcUserInfo::select('*', ['uid' => $uids]);
        $userMap = array_column($userInfoList, 'competition_name', 'uid');
        foreach ($list as &$item) {
            $item['nick'] = $userMap[$item['uid']];
            $item['honor_name'] = $honorNameToId[$item['honor_id']];
            $item['honor_type'] = self::HONOR_TYPE_TXT[$item['honor_type']] ?? '无';
            $item['work_name'] = $signDatasMap[$item['sign_id']]['work_name'] ?? '';
        }
        unset($item);
    }
}
